package com.hdkj.eduorder.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdkj.eduorder.entity.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118648268";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCLy4Df/4YPMx55QJRQbnHQcRSkEgfPMqGR9dC+nZhMQlxr1Q7qiYR8cuU/es1UWFYlxq1WAK0v1Q2Td3m2IfbJ2954Td3zCXHLbhyP28XUkftAOBhCXnTCfXw35HDS3OyI4MFez6E94bAUf0wE67mX3Een88WwKN+reR8uJGrN7rBAmp9IWjZ8eVBxM+Y4ElHXPaIpwGsDaz47MdRla8UzmK2Kdrqv2nmkSc2KbbloENh+xIpBSvxAavP7iQzXA8D1Uwxrhu2d9G6RkmQ6nMbsZteeOidZxPwh1U/ZYFSgQhk74usERKTaqGt6cbKbGlHPBdpgNR54iK2b8ssCloG5AgMBAAECggEAIjf8IdiATjQrH28JXu7J+UXU0ZyAQN2Z4wqSwDX/U5ti2Bpc/lgPEA9nwjQjEqRU4gZR451ixzSVkCVMq6KAsESbnkMhMx56tlsLrsMTF8dsAvo4JfbNCdaBk4TdgSAG3jJ14ulcBLO70CigyQuScHPBLoFjQI2gwm5xjW/onOOTAnyI5ytM7vw+iRsE284yQ3crItKQ5IB7m5FcA7C/RiT7I0sbzG5KuJp6HTTup+mLkaK9Jj2JAn6VNp39mryqha8KpCIC8ARYvCEwSF6pa0mQAl7ozANZYhHYgJnFAtydfYj0NtfMXmoPqONXQ8Uescq4WhxgXp9pk09shlf6AQKBgQDwd6cCW9moz/H2fJb/BLPfVVk6Pzgh/CXJb/nAuBQWXOI8mhn9qv8HTnJSWe/5LLcZQ09aKMpJC8az8HNKy7S1YM4tS6zSzHJjuo+2e2aaOCMsDVWVi//TwEu4iI3XVXWNNI8oquUVpz9wIAW9OZE964qz1Ogs1LVV2BnUC4eFWQKBgQCU0yPhSwjl2pjRNHPT2KVtiKriH50hkBsqXUi+aYIl52aZJiMctg37yMC+0y7zswBTdtlUFOKhhgjSpbMqTYfWBOcrGSOfZd2xmDNYJ5gxVTPBVXtrO/l6Yg94chMYE2X+mv8W5f72KL5CxD1sF3OnjObVZlyjlr5Oj8a2FUZzYQKBgE6q1jgvVEWlwcKhf8RuBWpKwaYMiJCVrMb5mvsxPWETge/Rei9CgawiPcQzKA6cUkoxewRD0+mRsK+tOgp+GeA8izbGSwOrXsDBh8JrHGXXd6oxso35/veHMT5tbfnEO3C2gR8MyWUMEfTchQQikSFwrS+nt0zzSV+MEucL3ooBAoGAW85T1+rdtk0+7+8XdJu0kPk50PwHhmjLGTsqYKvdaVxx4g+woUl9HtEcdZr438SQcuxh7IhltZZlYuxsta/WF6Q2plN/shi4xZFKOxtRuz0ADEebNu9sWp6bRIW6WaO9y2355jpDKb7Af5dm/IMLCVTWINZgV1nqU7RFtJUzPIECgYEA0ZswC36OsqJpzUvfu+9f2E7Un29E1LWWALBJLiD22Wu9cM1JAFG3qEwqfKQQOn3sAL0OFQk/vs+EizFXRL6jJ98Ct2Yq6MDAPR3bvwfqUlwSlcFvV71vpqf5fmDBJc3WA0NXUYDdEp+YXHJQVk4Y26kl1Zzm+k82ZmV9v2cHKiE=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApWhlU/I8tq4+d0kkCM8vOrzZw4H0sN85LI4GdAz81LrtuDy3oGFvloMqXgUl4oZQNykxVLQNtpjL1RYWLC/11yk5jJAhXyjAfcbVuvJ7NWoxq/FRl0DjZn+4fSbjHUo5TSkaunuo5KKsMbGuRm4yHfQ1qyAEiLL+JtjamxZj8g1unaNTZqCrk6t+CqvCZoP+/sQFd94qEqOwbxxfjfvyhmRTVV+/Ip8Ryb590bYiPJcBe6w3O6CUqV/+SUscn6TfCdQGT9Brzt49PN/IXjpbKAmkOxTzVmkCArvHi12V+mrzi3OIGo81r+rDj71xEt1ynycEHXSL8en5mtJVTu9v/QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页--项目的订单详细页面
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";



    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

       return_url="http://10.28.5.130:8007/eduorder/order/payok";
        notify_url="http://10.28.5.130:8007/eduorder/order/payok/";

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = String.valueOf(vo.getPrice());
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
